﻿#region Using Directives

    using System;
    using System.Linq;

    using Csla;

    using MarketPulse.BusinessLogic.Core;

#endregion

namespace MarketPulse.Reporting.Core
{
    /// <summary>
    /// This class represents a collection of Reporting Periods
    /// </summary>
    [Serializable]
    [Csla.Server.ObjectFactory("MarketPulse.DataAccess.Factory.ReportingPeriodFactory, MarketPulse.DataAccess.Factory", "FetchInfoList")]
    public class ReportingPeriodInfoList : MarketPulseReadOnlyListBase<ReportingPeriodInfoList, ReportingPeriodInfo>
    {

        #region Member Variables
           
            /// <summary>The language for the reporting periods in this list.</summary>
            private string mLanguage { get; set; }

        #endregion

        #region Constructors

        /// <summary>
            /// Default constructor is private in order to (try to) enforce construction
            /// only via the static factory methods provided.
            /// </summary>
            private ReportingPeriodInfoList()
            {
            }

        #endregion

        #region Methods

            #region Factory Methods

                /// <summary>
                /// Method used to get a read only list of read only ReportingPeriods that are associated with a particular Client (via the DataPortal client proxy)
                /// </summary>
                /// <param name="clientGUID">The GUID of the Client used to retrieve the list</param>
                /// <returns>A populated read only list of read only ReportingPeriods</returns>
                public static ReportingPeriodInfoList GetByClientGUID(Guid clientGUID)
                {
                    return DataPortal.Fetch<ReportingPeriodInfoList>(new SingleCriteria<Guid>(clientGUID));
                }

            #endregion

        #endregion

        #region Properties

            /// <summary>The current ReportingYear in use.</summary>
            public ReportingPeriodInfo ReportingYear { get; set; }

            /// <summary>
            /// The language for the reporting periods in this list
            /// </summary>
            public string Language
            {
                get { return mLanguage; }
                set
                {
                    mLanguage = value;

                    // Update child ReportingPeriods with this Language
                    foreach (var reportingPeriodInfo in this.Items)
                    {
                        reportingPeriodInfo.Language = mLanguage;
                    }
                }
            }

            /// <summary>
            /// The reporting periods in this list that represent years
            /// </summary>
            public IOrderedEnumerable<ReportingPeriodInfo> Years 
            {
                get 
                {
                    return from period in this.Items
                            where period.ReportingPeriodTypeGUID.Value.Equals(Guid.Parse("8E228EBE-0702-49F6-AEEE-91891842F1C5"))
                            orderby period.EffectiveEndDateTime descending
                            select period;
                }
            }

            /// <summary>
            /// The reporting periods in this list that represent actual reporting periods
            /// </summary>
            public IOrderedEnumerable<ReportingPeriodInfo> Periods
            {
                get
                {
                    return from period in this.Items
                           where period.EffectiveStartDateTime >= ReportingYear.EffectiveStartDateTime
                           && period.EffectiveEndDateTime <= ReportingYear.EffectiveEndDateTime
                           && !period.ReportingPeriodTypeGUID.Value.Equals(Guid.Parse("8E228EBE-0702-49F6-AEEE-91891842F1C5"))
                           orderby period.EffectiveEndDateTime descending
                           select period;
                }
            }

        #endregion
    }
}
